package cn.liziguo.tank.buff;

import cn.liziguo.tank.config.GameConfig;
import cn.liziguo.tank.constants.ImageConst;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.Effect;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import javafx.util.Duration;
import org.jetbrains.annotations.NotNull;

/**
 * @author Liziguo
 * @date 2022/7/11 17:14
 */
public class InvincibleEffect extends Effect {

    private AnimatedTexture animatedTexture;

    public InvincibleEffect() {
        super(GameConfig.INVINCIBLE_TIME);
    }

    public InvincibleEffect(@NotNull Duration duration) {
        super(duration);
    }


    @Override
    public void onEnd(@NotNull Entity entity) {
        entity.getViewComponent().removeChild(animatedTexture);
    }

    @Override
    public void onStart(@NotNull Entity entity) {
        AnimationChannel animationChannel = new AnimationChannel(FXGL.image(ImageConst.ARMED_HELMET), Duration.seconds(.75), 4);
        animatedTexture = new AnimatedTexture(animationChannel);
        animatedTexture.setTranslateX(entity.getWidth() / 2 - animatedTexture.getFitWidth() / 2);
        animatedTexture.setTranslateY(entity.getHeight() / 2 - animatedTexture.getFitHeight() / 2);
        entity.getViewComponent().addChild(animatedTexture.loop());

    }
}
